package com.example.yjhero.safeapp.activity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.yjhero.safeapp.R;
import com.example.yjhero.safeapp.base.BaseSetUpActivity;
import com.example.yjhero.safeapp.utils.DialogUtil;
import com.example.yjhero.safeapp.utils.LogUtil;

public class SetUp2Activity extends BaseSetUpActivity {
    private TelephonyManager mTelephonyManager;
    private Button mBindSIMBtn;

    @SuppressLint("ServiceCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup2);
        mTelephonyManager= (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        initView();
        initData();
    }

    private void initData() {
        mBindSIMBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isBind()){
                    @SuppressLint("MissingPermission") String simSeriaNumber=mTelephonyManager.getSimSerialNumber();
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("sim",simSeriaNumber);
                    editor.commit();
                    Toast.makeText(SetUp2Activity.this,"SIM卡绑定成功!",Toast.LENGTH_LONG).show();
                    //DialogUtil.showInToast("SIM卡绑定成功!");
                    mBindSIMBtn.setEnabled(false);
                }else {
                    DialogUtil.showInToast("SIM卡已经绑定!");
                    mBindSIMBtn.setEnabled(false);
                }
            }
        });
        if(isBind()){
            mBindSIMBtn.setEnabled(false);
        }else{
            mBindSIMBtn.setEnabled(true);
        }
    }

    private boolean isBind() {
        String simString=sp.getString("sim",null);
        LogUtil.logI("SetUp2Activity","isBind |",simString+"");
        if(TextUtils.isEmpty(simString)){
            return false;
        }
        return true;
    }

    private void initView() {
        ((RadioButton)findViewById(R.id.rb_second)).setChecked(true);
        mBindSIMBtn=findViewById(R.id.btn_bind_sim);
    }

    @Override
    public void showNext() {
        if(!isBind()){
           DialogUtil.showInToast(this,"您还没有绑定SIM卡");
            return;
        }
        startActivityAndFinishSelf(SetUp3Activity.class);
    }

    @Override
    public void showPre() {
        startActivityAndFinishSelf(Setup1Activity.class);
    }
}
